﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace newTurtle8
{
    public partial class TahasukaAlalCard : UserControl
    {
        static readonly public string TableName = "TahasukaAlal";
        Tahasuka tahasuka;
        DataTable dt = new DataTable();
        SqlCommand selectCmd;
        SqlCommand updateCmd;
       
        public TahasukaAlalCard(Tahasuka tahasuka)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.tahasuka = tahasuka;
            tahasuka.ZimunimList_Modified += new Mofa.ZimunimListChangedHandler(refresh);

            selectCmd = SqlManager.getSqlCommandFromFile(@"\SELECT\TahasukaAlal.SELECT.sql");
            updateCmd = SqlManager.getSqlCommandFromFile(@"\UPDATE\TahasukaAlal.UPDATE.sql");

            ICollection<string> sqlParameterCollection = SqlManager.parseSqlQueryParameters(updateCmd.CommandText);
            sqlParameterCollection.Distinct<string>();

            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
            {
                string sItem = "@" + item;
                switch (item)
                {
                    case "armyID": 
                        updateCmd.Parameters.Add(SqlManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Original, "armyID"));
                        break;

                    case "startDate":
                        selectCmd.Parameters.Add(sItem, SqlDbType.Date).Value = tahasuka.StartDate;
                        updateCmd.Parameters.Add(sItem, SqlDbType.Date).Value = tahasuka.StartDate;
                        break;

                    default: 
                        updateCmd.Parameters.Add(SqlManager.createSqlParameter("@" + item, SqlDbType.Bit, DataRowVersion.Current, item)).Value = false;
                        break;
                }
            }

            refresh();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            SqlManager.updateSqlNonQuery(updateCmd, dt);
        }

        public void refresh()
        {
            //Populate the DataTable.
            dt.Clear();
            dt = SqlManager.selectSqlQueryDataTable(selectCmd);
            dataGridView.DataSource = dt;
            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dataGridView.Columns["firstName"].ReadOnly = true;
            dataGridView.Columns["lastName"].ReadOnly = true;
            dataGridView.Columns["armyID"].ReadOnly = true;
            dataGridView.Columns["startDate"].Visible = false;

            Translator.setTableColumnHeadersHebrew(dataGridView.Columns);
        }
    }
}
